// pages/action/action.js
import {
  HTTP
} from '../../utils/http.js'
const app = getApp()
let http = new HTTP()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    countList: ['3', '5', '10'],
    totalCount: 5,
    userInfo: null,
    records: [],
    currentIndex: 0,
    checkAnswers: [],
    answerShow: false,
    wrongCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    this.getQuestionList()
  },

  /**
   * 选择题数
   */
  showSheet(e) {
    let that = this
    wx.showActionSheet({
      itemList: that.data.countList,
      success(res) {
        console.log(res.tapIndex)
        that.setData({
          totalCount: that.data.countList[res.tapIndex]
        })
        that.getQuestionList()
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  /**
   * 获取考题
   */
  getQuestionList() {
    wx.showLoading({
      title: '试卷生成中',
    })
    http.request({
      url: '/paper/t-paper/createPaper',
      data: {
        uid: this.data.userInfo.id,
        num: this.data.totalCount
      },
      success: res => {
        this.setData({
          paperId: res.model.paperId,
          records: res.model.records,
          currentQues: res.model.records[this.data.currentIndex]
        })
        wx.hideLoading()
      }
    })
  },

  /**
   * 选择答案
   */
  radioChange(e) {
    const checkedAnswer = e.detail.value
    let records = this.data.records
    records[this.data.currentIndex].checkedAnswer = checkedAnswer
    let checkAnswers = this.data.checkAnswers
    checkAnswers[this.data.currentIndex] = checkedAnswer
    let currentQues = this.data.currentQues
    currentQues.checkedAnswer = checkedAnswer
    this.setData({
      records,
      checkAnswers,
      currentQues
    })
    let that = this
    // setTimeout(function() {
    //   that.next()
    // }, 300)
  },

  /**
   * 显示解析
   */
  showAnalysis(e) {
    this.setData({
      answerShow: true
    })
  },

  /**
   * 改变当前题目
   */
  changeQues(e) {
    const type = e.currentTarget.dataset.type
    if (type == 'prev') {
      this.prev()
    } else {
      this.next()
    }
  },

  /**
   * 上一题
   */
  prev() {
    if (this.data.currentIndex == 0) {
      return false
    }
    this.setData({
      currentIndex: this.data.currentIndex - 1,
      currentQues: this.data.records[this.data.currentIndex - 1],
      answerShow: false
    })
  },

  /**
   * 下一题
   */
  next() {
    // 未选中答案， 不能进行下一题
    if (!this.data.currentQues.checkedAnswer) {
      return false
    }
    // 对所选答案进行判断
    const rightAnswer = this.data.currentQues.rightAnswerId
    const checkedAnswer = this.data.currentQues.checkedAnswer
    if (rightAnswer == checkedAnswer || this.data.answerShow) { // 选中正确答案 或 已查看正确答案
      if (this.data.currentIndex + 1 == this.data.totalCount) { // 当前为最后一题时， 提交试卷
        this.submit()
      } else { // 当前不是最后一题时， 显示下一题
        this.setData({
          currentIndex: this.data.currentIndex + 1,
          currentQues: this.data.records[this.data.currentIndex + 1],
          answerShow: false
        })
      }
    } else { // 未选中正确答案， 显示正确答案和解析
      this.setData({
        answerShow: true,
        wrongCount: this.data.wrongCount + 1
      })
    }
  },

  /**
   * 提交试卷
   */
  submit() {
    wx.showLoading({
      title: '提交试卷中',
    })
    http.request({
      url: '/paper/t-paper/finishPaper',
      method: 'POST',
      data: {
        paperId: this.data.paperId,
        records: JSON.stringify(this.data.records),
        wrongCount: this.data.wrongCount,
        rightCount: this.data.totalCount - this.data.wrongCount,
        // TODO spendTime
      },
      success: res => {
        console.log(res)
        // 计算正确率
        const rate = ((this.data.totalCount - this.data.wrongCount) / this.data.totalCount).toFixed(2)
        wx.navigateTo({
          url: `/pages/result/result?rate=${rate * 100}`,
        })
      },
      fail: err => {

      }
    })
  }
})